package com.LC.Service.Impl;

import com.LC.DAO.CheckItemDAO;
import com.LC.Entity.CheckItem;
import com.LC.Entity.PageResult;
import com.LC.Entity.QueryPageBean;
import com.LC.Service.CheckItemService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDAO checkItemDAO;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDAO.add(checkItem);
    }

    /**
     * 分页方法
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //使用mybatis分页助手
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page = checkItemDAO.findPage(queryString);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * 由id删除检查项
     * @param id
     */
    @Override
    public void deleteById(int id) {
        //判断检查组是否包含要删除的检查项
        if (checkItemDAO.itemInCheckGroup(id) > 0){
            //检查组包含该检查项，不可删除
             throw new RuntimeException();
        }else {
            //如果检查组不包含检查项，删除
            checkItemDAO.deleteById(id);
        }
    }

    @Override
    public CheckItem itemById(int id) {
        return checkItemDAO.itemById(id);
    }

    @Override
    public void newItemData(CheckItem checkItem) {
        checkItemDAO.newItemData(checkItem);
    }


}
